package com.wlyuan.open.scanner.domain.extension;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wlyuan.open.scanner.dao.entity.ExtensionDO;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author lijing
 * @description
 */
public class ExtensionConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = MappingInvalid.class)
    public interface Convert {

        @Mappings({
                @Mapping(target = "id", source = "extensionId"),
                @Mapping(target = "sourceKey", source = "extensionId"),
                @Mapping(target = "orderId", source = "extensionOrdercid"),
                @Mapping(target = "pickAddrCode", source = "extensionThirdone"),
                @Mapping(target = "fromShortName", source = "extensionThirdtwo"),
                @Mapping(target = "deliverAddrCode", source = "extensionThirdthree"),
                @Mapping(target = "deliverShortName", source = "extensionThirdfour"),
                @Mapping(target = "zoneCode", source = "extensionThirdfive"),
                @Mapping(target = "zoneName", source = "extensionThirdsix"),
                @Mapping(target = "orderTypeName", source = "extensionThirdseven"),
                @Mapping(target = "branchFactoryCode", source = "extensionThirdeight"),
                @Mapping(target = "branchFactoryName", source = "extensionThirdnine"),
                @Mapping(target = "allotNumber", source = "extensionThirdeleven"),

                @Mapping(target = "customerCode", source = "extensionThirdthirteen"),
                @Mapping(target = "customerName", source = "extensionThirdfourteen"),
                @Mapping(target = "pickCode", source = "extensionThirdfifteen"),
                @Mapping(target = "pickShortName", source = "extensionThirdsixteen"),
                @Mapping(target = "arriveCode", source = "extensionThirdseventeen"),
                @Mapping(target = "arriveName", source = "extensionThirdeighteen"),
                @Mapping(target = "mainContractNo", source = "extensionThirdtwentytwo"),

                @Mapping(target = "createAt", source = "extensionInserttime"),
                @Mapping(target = "updateAt", source = "extensionInserttime"),
                @Mapping(target = "createBy", constant = "0L"),
                @Mapping(target = "updateBy", constant = "0L"),
                @Mapping(target = "batchNo", source = "extensionThirdtwentythree"),
                @Mapping(target = "combine", source = "extensionThirdnineteen"),
                @Mapping(target = "orderNo", constant = ""),
        })
        @BaseMapping
        Extension convertTop(ExtensionDO extensionDO);

        @AfterMapping
        default void after(@MappingTarget Extension target, ExtensionDO source) {
            target.setRaw(JsonUtils.withNullJson(source));
            if (StringUtils.isNotBlank(source.getExtensionThirdtwentyfive())) {
                JSONObject object = JSON.parseObject(source.getExtensionThirdtwentyfive());
                target.setOriginNumber(object.getString("zysdd"));
                target.setDeliverOrderNo(object.getString("vbeln"));
                target.setOrderTypeCode(object.getString("bsart"));
                target.setDate(object.getString("bedat"));
                target.setFromFactory(object.getString("item-zwerks"));
                target.setToFactory(object.getString("item-werks"));
                target.setFromHouse(object.getString("item-reslo"));
                target.setToHouse(object.getString("item-lgort"));
                target.setPhysical(object.getString("sortl2"));
            } else {
                target.setOriginNumber("");
                target.setDeliverOrderNo("");
                target.setOrderTypeCode("");
                target.setDate("");
                target.setFromFactory("");
                target.setToFactory("");
                target.setFromHouse("");
                target.setToHouse("");
                target.setPhysical("");
            }
            try {
                var vehicle = source.getExtensionThirdten();
                if (StringUtils.isNotBlank(vehicle) && vehicle.contains("*")) {
                    var split = vehicle.split("\\*");
                    var count = split[0];
                    var load = split[1];
                    if (StringUtils.isNotBlank(count) && !"null".equals(count)) {
                        target.setVehicleCount(new BigDecimal(count).intValue());
                    }
                    if (StringUtils.isNotBlank(load) && !"null".equals(load)) {
                        target.setVehicleLoad(load);
                    }
                }
            } catch (Exception e) {
            }
        }

        List<Extension> convertTop(List<ExtensionDO> list);

    }

    public static List<Extension> convertTop(List<ExtensionDO> creation) {
        return CONVERT.convertTop(creation);
    }

    public static Extension convertTop(ExtensionDO creation) {
        return CONVERT.convertTop(creation);
    }

}
